package com.zjlanyun.erp.rfid.m10.u8.activity;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zjlanyun.erp.MainApplication;
import com.zjlanyun.erp.R;
import com.zjlanyun.erp.rfid.m10.u8.reader.model.ReaderSetting;
import com.zjlanyun.erp.rfid.m10.u8.reader.server.ReaderBase;
import com.zjlanyun.erp.rfid.m10.u8.reader.server.ReaderHelper;
import com.zjlanyun.erp.utils.Toastor;
import com.zjlanyun.erp.view.BaseSwipeBackActivity;

public class BeeperActivity extends BaseSwipeBackActivity {

    private TextView mSet;

    private RadioGroup mGroupBeeper;
    private CheckBox mCloseSound;

    private ReaderHelper mReaderHelper;
    private ReaderBase mReader;

    private static ReaderSetting m_curReaderSetting;
    private LocalBroadcastManager lbm;

    @Override
    protected void onResume() {
        if (mReader != null) {
            if (!mReader.IsAlive())
                mReader.StartWait();
        }
        super.onResume();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rfid_setting_beeper);
        initBaseView();
        ((MainApplication) getApplication()).addActivity(this);

        try {
            mReaderHelper = ReaderHelper.getDefaultHelper();
            mReader = mReaderHelper.getReader();
        } catch (Exception e) {
            e.printStackTrace();
        }

        m_curReaderSetting = mReaderHelper.getCurReaderSetting();

        title.setText("蜂鸣设置");
        mSet = (Button) findViewById(R.id.set);
        mGroupBeeper = (RadioGroup) findViewById(R.id.group_beeper);
        mCloseSound = (CheckBox) findViewById(R.id.cb_close_soft_sound);

        mCloseSound.setChecked(MainApplication.getSoftSound() == 0 ? true : false);
        mSet.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                byte btMode = 0;
                int checkedRadioButtonId = mGroupBeeper.getCheckedRadioButtonId();
                if (checkedRadioButtonId == R.id.set_beeper_quiet) {
                    btMode = 0;
                } else if (checkedRadioButtonId == R.id.set_beeper_all) {
                    btMode = 1;
                } else if (checkedRadioButtonId == R.id.set_beeper_one) {
                    btMode = 2;
                }
                mReader.setBeeperMode(m_curReaderSetting.btReadId, btMode);
                m_curReaderSetting.btBeeperMode = btMode;
                MainApplication.saveBeeperState((btMode & 0xFF));
                MainApplication.saveSoftSound(mCloseSound.isChecked() ? 0 : 1);
            }
        });

        lbm = LocalBroadcastManager.getInstance(this);

        IntentFilter itent = new IntentFilter();
        itent.addAction(ReaderHelper.BROADCAST_WRITE_LOG);

        lbm.registerReceiver(mRecv, itent);

        updateView();
    }

    public void updateView() {
        m_curReaderSetting.btBeeperMode = (byte) MainApplication.getVeeperState();
        if (m_curReaderSetting.btBeeperMode == 0) {
            mGroupBeeper.check(R.id.set_beeper_quiet);
        } else if (m_curReaderSetting.btBeeperMode == 1) {
            mGroupBeeper.check(R.id.set_beeper_all);
        } else if (m_curReaderSetting.btBeeperMode == 2) {
            mGroupBeeper.check(R.id.set_beeper_one);
        }
    }

    private final BroadcastReceiver mRecv = new BroadcastReceiver() {
        @Override//buzzer
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ReaderHelper.BROADCAST_WRITE_LOG)) {
                Toastor.getInstance().showShortToast("参数设置成功");
                //mLogList.writeLog((String)intent.getStringExtra("log"), intent.getIntExtra("type", ERROR.SUCCESS));
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (lbm != null)
            lbm.unregisterReceiver(mRecv);
    }
}

